package com.sina.shop.controller.goods;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.goods.GoodsComment;
import com.sina.shop.service.goods.GoodsCommentService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-18:17
 */
@Controller
@RequestMapping("/shop-web/goodsComment")
public class GoodsCommentController extends BaseController {

    @Autowired
    private GoodsCommentService goodsCommentService;

    @RequestMapping(value = "/updateGoodsComment", method = RequestMethod.POST)
    public ModelAndView updateGoodsComment(GoodsComment goodsComment) {
        LOG.info("goodsComment:{}", goodsComment);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsCommentService.updateGoodsComment(goodsComment);
            result.put(CODE, OK);
            result.put(MSG, "更新商品评论成功");
        } catch (UpdateException e) {
            LOG.info("更新商品评论出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/getGoodsComment", method = RequestMethod.GET)
    public ModelAndView getGoodsComment(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        GoodsComment goodsComment = goodsCommentService.getGoodsComment(id);

        data.put("goodsComment", goodsComment);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getGoodsCommentList", method = RequestMethod.GET)
    public ModelAndView getGoodsCommentList(GoodsComment goodsComment) {
        LOG.info("goodsComment:{}", goodsComment);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(6);
        Map<String,Object> map = goodsCommentService.getGoodsCommentList(goodsComment);

        result.put(CODE, OK);
        result.put(DATA, map);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveGoodsComment", method = RequestMethod.POST)
    public ModelAndView saveGoodsComment(GoodsComment goodsComment) {
        LOG.info("goodsComment:{}", goodsComment);
        goodsComment.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsCommentService.saveGoodsComment(goodsComment);
            result.put(CODE, OK);
            result.put(MSG, "新增商品评论成功");
        } catch (UpdateException e) {
            LOG.info("新增商品评论出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteGoodsComment", method = RequestMethod.POST)
    public ModelAndView deleteGoodsComment(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            goodsCommentService.deleteGoodsComment(id);
            result.put(CODE, OK);
            result.put(MSG, "删除商品评论成功");
        } catch (UpdateException e) {
            LOG.info("删除商品评论出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
